//
//  TGDock.m
//  GroupBuy
//
//  Created by wwj on 15/10/30.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGDock.h"
#import "TGTabItem.h"
#import "TGMoreItem.h"
#import "TGLocationItem.h"

@interface TGDock ()
@property (weak ,nonatomic) UIButton *selectBtn;
@end

@implementation TGDock

+(instancetype)dock{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_tabbar"]];
        self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        
        //添加logo
        [self addLogo];
        
        //添加选项卡
        [self addTabCard];
        
        //添加定位
        [self addLocation];
        
        //添加更多
        [self addMore];
        
    }
    return self;
}


#pragma -mark 添加Logo
- (void)addLogo{
    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_logo"]];
    CGFloat scale = 0.65;
    logo.bounds = CGRectMake(0, 0, logo.frame.size.width*scale, logo.frame.size.height*scale);
    logo.center = CGPointMake(kDockW * 0.5, kDockH * 0.5);
    [self addSubview:logo];
}

#pragma -mark 添加选项卡
- (void)addTabCard{
    //添加团购
    [self addTabCardWithIcon:@"ic_deal" selectIcon:@"ic_deal_hl" index:1];
    //添加地图
    [self addTabCardWithIcon:@"ic_map" selectIcon:@"ic_map_hl" index:2];
    //添加收藏
    [self addTabCardWithIcon:@"ic_collect" selectIcon:@"ic_collect_hl" index:3];
    //添加我的
    [self addTabCardWithIcon:@"ic_mine" selectIcon:@"ic_mine_hl" index:4];
    
    //添加标签底部的分隔线
    UIImageView *divider = [[UIImageView  alloc] init];
    divider.frame = CGRectMake(0, kDockH * 5, kDockW, 2);
    divider.image = [UIImage imageNamed:@"separator_tabbar_item"];
    [self addSubview:divider];
}

#pragma -mark 添加选项卡
- (void)addTabCardWithIcon:(NSString *)icon selectIcon:(NSString *)selectIcon index:(int)index{
    TGTabItem *btn = [TGTabItem tabItem];
    btn.frame = CGRectMake(0, kDockH * index, 0, 0);
    btn.tag = index - 1;
    [btn setIcon:icon selectIcon:selectIcon];
    [self addSubview:btn];
    [btn addTarget:self action:@selector(onClickDockItem:) forControlEvents:UIControlEventTouchDown];
    
    if (index == 1) {
        [self onClickDockItem:btn];
    }
}



#pragma -mark 添加定位
- (void)addLocation{
    TGLocationItem *locationItem = [TGLocationItem locationItem];
    locationItem.frame = CGRectMake(0, self.frame.size.height - 2 * kDockH, 0, 0);
    [self addSubview:locationItem];
}

#pragma -mark 添加更多
- (void)addMore{
    TGMoreItem *moreItem = [TGMoreItem moreItem];
    moreItem.frame =CGRectMake(0, self.frame.size.height - kDockH, 0, 0);
    [self addSubview:moreItem];
}

-(void)onClickDockItem:(UIButton*)btn{
    
    if ([_delegate respondsToSelector:@selector(onClickDockTab:from:to:)]) {
        [_delegate onClickDockTab:self from:_selectBtn.tag to:btn.tag];
    }
    
    _selectBtn.enabled = YES;
    btn.enabled = NO;
    _selectBtn = btn;
    
}

#pragma -mark 设置宽度
-(void)setFrame:(CGRect)frame{
    frame.size.width = kDockW;
    [super setFrame:frame];
}

@end
